package com.snut.tomorrowNews.controller.front;

import com.snut.tomorrowNews.common.CommonResult;
import com.snut.tomorrowNews.model.Comment;
import com.snut.tomorrowNews.model.User;
import com.snut.tomorrowNews.service.front.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    /**
     * 用户发起评论
     */
    @RequestMapping("/sendComment")
    public CommonResult sendComment(Comment comment, HttpSession session) {
        CommonResult commonResult = null;
        try {
            User user = (User) session.getAttribute("user");
            System.out.println(user.getState());
            if (user.getState()==1){
                commonResult = new CommonResult<>(201, "您已被禁言", null);
            }else {
                Integer userId = user.getId();
                comment.setCommenterId(userId);
                commentService.sendComment(comment);
                commonResult = new CommonResult<>(200, "发起评论成功,请等到审核", null);
            }
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }

    /**
     * 按照新闻id查找评论
     */
    @RequestMapping("getCommentByNewsId")
    public CommonResult<List<Comment>> getCommentByNewsId(Integer newsId){
        CommonResult<List<Comment>> commonResult = null;
        try {
            List<Comment> comments = commentService.getCommentByNewsId(newsId);
            commonResult = new CommonResult<>(200, "查找成功", comments);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }


    /**
     * 查找所有未审核评论
     */
    @RequestMapping("getAllComment")
    public CommonResult<List<Comment>> getAllComment(){
        CommonResult<List<Comment>> commonResult = null;
        try {
            List<Comment> comments = commentService.getAllComment();
            commonResult = new CommonResult<>(200, "查找成功", comments);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }


    /**
     * 审核删除评论
     */
    @RequestMapping("deleteComment")
    public CommonResult deleteComment(Integer id){
        CommonResult commonResult = null;
        try {
            commentService.deleteComment(id);
            commonResult = new CommonResult<>(200, "删除", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }


    @RequestMapping("passComment")
    public CommonResult passComment(Integer id){
        CommonResult commonResult = null;
        try {
            commentService.passComment(id);
            commonResult = new CommonResult<>(200, "通过成功", null);
        } catch (Exception e) {
            commonResult = new CommonResult<>(500, "服务器异常", null);
            e.printStackTrace();
        }
        return commonResult;
    }



}
